/*
 * Copyright 2009 Kaspar Minosiants http://www.minosiants.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.minosiants.confluence.blog.plugin.tweet2it.macro;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.atlassian.confluence.core.ContentEntityObject;
import com.atlassian.confluence.renderer.PageContext;
import com.atlassian.confluence.renderer.radeox.macros.MacroUtils;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.util.velocity.VelocityUtils;
import com.atlassian.renderer.RenderContext;
import com.atlassian.renderer.v2.RenderMode;
import com.atlassian.renderer.v2.macro.BaseMacro;
import com.atlassian.renderer.v2.macro.MacroException;
import com.atlassian.user.User;
import com.minosiants.confluence.blog.plugin.tweet2it.IContextUtil;
import com.minosiants.confluence.blog.plugin.tweet2it.IHttpClient;
import com.opensymphony.webwork.ServletActionContext;
/**
 *	Project home: http://tweet2it.minosiants.com
 *
 *  @author Kaspar Minosiants
 *
 * 	$Id:$
 */
public class TwitterMacro extends BaseMacro{
	private Log logger = LogFactory.getLog(getClass());
	private SpaceManager spaceManager;
	private IHttpClient httpClient;
	public SpaceManager getSpaceManager() {return spaceManager;}

	public void setSpaceManager(SpaceManager spaceManager) {this.spaceManager = spaceManager;}

	public String execute(Map parametrs, String arg1, RenderContext renderContext)throws MacroException {
		 // retrieve a reference to the body object this macro is in
        PageContext pageContext = ( PageContext ) renderContext;
        ContentEntityObject content = pageContext.getEntity();
        String twitterUser=(String)parametrs.get(IContextUtil.Constants.TWITTER_USER);
        String confluenceUser=(String)parametrs.get(IContextUtil.Constants.CONFLUENCE_USER); 
        ServletActionContext.getRequest().getSession().setAttribute(IContextUtil.Constants.TWITTER_USER, twitterUser);
        ServletActionContext.getRequest().getSession().setAttribute(IContextUtil.Constants.CONFLUENCE_USER, confluenceUser);
        User user = AuthenticatedUserThreadLocal.getUser();       
		 Map<String, Object> contextMap = MacroUtils.defaultVelocityContext();
		 try {
			 	Boolean isReadOnly=isReadOnly(user, confluenceUser);
			 	logger.debug("isReadOnly: "+isReadOnly);
			 	contextMap.put( "readOnly", isReadOnly);			 	
			 	ServletActionContext.getRequest().getSession().setAttribute(IContextUtil.Constants.READ_ONLY, isReadOnly);
	            return VelocityUtils.getRenderedTemplate( "templates/extra/tweet2it/tweet2it.vm", contextMap );
	        } catch ( Exception e ) {
	        	logger.error( "Error while trying to display twitter content!", e );
	            throw new MacroException( e.getMessage() );
	        }		
	}
	public Boolean isReadOnly(User user, String confluenceUser) {
	    return user==null || !user.getName().equals(confluenceUser); 
	}
	public RenderMode getBodyRenderMode() {
		return RenderMode.NO_RENDER;
	}

	public boolean hasBody() {

		return false;
	}

	public boolean isInline() {

		return false;
	}

	public void setHttpClient(IHttpClient httpClient) {
		this.httpClient = httpClient;
	}

}
